package cl.clientews.soap;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class PublicacionesSOAWS {
	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;

	/**
	 * Datos para la conexion al webservice de PublicacionesSOA
	 */
	public void coneccionws(){

		boolean londres = true;

		if(londres){
			NAMESPACE = "http://ws"; 
			URL = "http://200.13.4.205:8080/SOA_OFERTA/services/PublicacionSOA?wsdl";
		}else{
			NAMESPACE = "http://ws";
			URL = "http://186.10.30.33:8080/SOA_OFERTA/services/PublicacionSOA";
		}
	}

	/**
	 * 
	 * @param pub_mon
	 * @param pub_des
	 * @param pers_rut
	 * @return
	 */
	public String addPublicacion(String pub_mon, String pub_des, String pers_rut){

		coneccionws();

		METHOD_NAME = "add";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo addmonto = new PropertyInfo();
		PropertyInfo adddes = new PropertyInfo();
		PropertyInfo addrut = new PropertyInfo();

		addmonto.setName("pub_mon");
		addmonto.setValue(pub_mon);
		addmonto.setType(String.class);

		adddes.setName("pub_des");
		adddes.setValue(pub_des);
		adddes.setType(String.class);

		addrut.setName("pers_rut");
		addrut.setValue(pers_rut);
		addrut.setType(String.class);

		request.addProperty(addmonto);
		request.addProperty(adddes);
		request.addProperty(addrut);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;
	}

	/**
	 * 
	 * @param id
	 * @return
	 */
	public String deletePublicacion(String id){

		coneccionws();

		METHOD_NAME = "delete";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo delete = new PropertyInfo();

		delete.setName("id");
		delete.setValue(id);
		delete.setType(String.class);

		request.addProperty(delete);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}
	/**
	 * 
	 * @param id
	 * @param campo
	 * @param new_valor
	 * @return
	 */
	public String updatePublicacion(String id, int campo, String new_valor){

		coneccionws();

		METHOD_NAME = "update";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

		PropertyInfo updateid = new PropertyInfo();
		PropertyInfo updatecampo = new PropertyInfo();
		PropertyInfo updatenew = new PropertyInfo();

		updateid.setName("id");
		updateid.setValue(id);
		updateid.setType(String.class);

		updatecampo.setName("campo");
		updatecampo.setValue(campo);
		updatecampo.setType(int.class);

		updatenew.setName("new_valor");
		updatenew.setValue(new_valor);
		updatenew.setType(String.class);

		request.addProperty(updateid);
		request.addProperty(updatecampo);
		request.addProperty(updatenew);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;

	}

	/**
	 * 
	 * @param buscar
	 * @return
	 */
	public String findPublicacion(String buscar){

		coneccionws();

		METHOD_NAME = "find";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo find = new PropertyInfo();

		find.setName("buscar");
		find.setValue(buscar);
		find.setType(String.class);

		request.addProperty(find);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}
	/**
	 * 
	 * @param buscar
	 * @return
	 */
	public String finAllPublicacion(String buscar){

		coneccionws();

		METHOD_NAME = "findall";

		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findall = new PropertyInfo();

		findall.setName("buscar");
		findall.setValue(buscar);
		findall.setType(String.class);

		request.addProperty(findall);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		HttpTransportSE aht = new HttpTransportSE(URL);

		String datosJsonString;
		try
		{
			aht.call(SOAP_ACTION, soapEnvelope);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
			datosJsonString =resultString.toString();
			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";
			return datosJsonString;}
		


	}

}
